package com.example.librarymanagementsystem.severice;

import com.example.librarymanagementsystem.enums.BookStatus;
import com.example.librarymanagementsystem.mapper.BookMapper;
import com.example.librarymanagementsystem.model.BookInfo;
import com.example.librarymanagementsystem.model.PageRequest;
import com.example.librarymanagementsystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//将类交给spring管理
@Service
public class BookSeverice {
    @Autowired
    private BookMapper mapper;



    public Integer insertBook(BookInfo bookInfo) {
        int data = mapper.insertBook(bookInfo);
        return data;
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest request) {
        //1、获取总记录数
        Integer count = mapper.selectCount();
        //2、获取当前页记录
        List<BookInfo> bookInfos = mapper.queryBookByPage(request.getOffset(), request.getPageSize());
        // 3、处理状态
        //4、0-》删除   1-》可借阅    2-》不可借阅
        for (BookInfo bookInfo : bookInfos) {
            bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos, count,request);
    }

    //根据id查询图书
    public BookInfo queryBookById(Integer bookId) {
       BookInfo bookInfo =  mapper.queryById(bookId);
       bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
       return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return mapper.updateBookById(bookInfo);
    }

    public Integer deleteBookById(Integer bookId) {
        return mapper.deleteBookById(bookId);
    }

    public Integer batchDeleteById(List<Integer> ids) {
        return mapper.batchDeleteBookById(ids);
    }
}

